package com.gc.web.controller.business;

import java.util.List;
import java.util.Arrays;

import com.gc.business.domain.ElevatorInfo;
import com.gc.business.service.IElevatorInfoService;
import com.gc.common.base.Result;
import com.gc.common.support.Convert;
import com.gc.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gc.common.annotation.Log;
import com.gc.common.enums.BusinessType;
import com.gc.business.domain.UserElevatorAllot;
import com.gc.business.service.IUserElevatorAllotService;
import com.gc.framework.web.base.BaseController;
import com.gc.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 员工电梯分配 控制层
 * 
 * @author hruiyan
 * @date 2020-11-17
 */
@Slf4j
@Controller
@RequestMapping("/business/userElevatorAllot")
public class UserElevatorAllotController extends BaseController {
    private String prefix = "business/userElevatorAllot";
	
	private final IUserElevatorAllotService userElevatorAllotService;
	private final IElevatorInfoService elevatorInfoService;

	@Autowired
	public UserElevatorAllotController(IUserElevatorAllotService userElevatorAllotService,
									   IElevatorInfoService elevatorInfoService) {
		this.userElevatorAllotService = userElevatorAllotService;
		this.elevatorInfoService = elevatorInfoService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:userElevatorAllot:view")
	@GetMapping
	public String userElevatorAllot(){
	    return prefix + "/userElevatorAllot";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(Long userId,ModelMap mmap){
		//查询所归属管理旗下的小区电梯列表
		List<ElevatorInfo> elevatorInfoList = elevatorInfoService.selectlistByUserId(userId);

		UserElevatorAllot userElevator = new UserElevatorAllot().setUserId(userId);
		List<UserElevatorAllot> list = userElevatorAllotService.list(userElevator);
		if(list.size() > 0 && StringUtils.isNotNull(list)){
			for (ElevatorInfo elevatorInfo : elevatorInfoList) {
				for (UserElevatorAllot userElevatorAllot : list) {
					if(userElevatorAllot.getElevatorId().longValue() == elevatorInfo.getElevatorId().longValue()){
						elevatorInfo.setOffChecked(true);
					}
				}
			}
		}

		mmap.put("elevatorInfoList",elevatorInfoList);
		mmap.put("userId",userId);
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{allotId}")
	public String edit(@PathVariable("allotId") Long allotId, ModelMap mmap){
		UserElevatorAllot userElevatorAllot = userElevatorAllotService.getById(allotId);
		mmap.put("userElevatorAllot", userElevatorAllot);
		return prefix + "/edit";
	}
	
	/**
	 * 查询员工电梯分配列表
	 */
	@RequiresPermissions("business:userElevatorAllot:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(UserElevatorAllot userElevatorAllot){
		startPage();
        List<UserElevatorAllot> list = userElevatorAllotService.list(userElevatorAllot);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存员工电梯分配
	 */
	@Log(title = "员工电梯分配", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(Long userId,String elevators){
		userElevatorAllotService.saveElevatorAllot(userId,elevators);
		return Result.success();
	}

	/**
	 * 修改保存员工电梯分配
	 */
	@RequiresPermissions("business:userElevatorAllot:edit")
	@Log(title = "员工电梯分配", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(Long userId,String elevators){
		userElevatorAllotService.saveElevatorAllot(userId,elevators);
		return Result.success();
	}
	
	/**
	 * 删除员工电梯分配
	 */
	@RequiresPermissions("business:userElevatorAllot:remove")
	@Log(title = "员工电梯分配", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		userElevatorAllotService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出员工电梯分配列表
	 */
	@RequiresPermissions("business:userElevatorAllot:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(UserElevatorAllot userElevatorAllot){
		List<UserElevatorAllot> list = userElevatorAllotService.list(userElevatorAllot);
		ExcelUtil<UserElevatorAllot> util = new ExcelUtil<>(UserElevatorAllot.class);
		util.exportExcel(list, "userElevatorAllot");
		return Result.success();
	}
}
